﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRFEncoder
{
    public class BigArray<T>
    {
        const long sizePerBlock = 1024 * 1024 * 400;
        long size_;
        long lowBounding_;
        private List<T[]> arrList;
     
        public T this[long i]
        {
            get
            {
                long nBlock = (i - lowBounding_) / sizePerBlock;
                return arrList[(int)nBlock][(i - lowBounding_) % sizePerBlock];
            }
            set
            {
                long nBlock = (i - lowBounding_) / sizePerBlock;
                arrList[(int)nBlock][(i - lowBounding_) % sizePerBlock] = value;
            }
        }

        public long Length
        {
            get
            {
                return size_;
            }
        }

        public void Clear()
        {
            for (int i = 0;i < arrList.Count;i++)
            {
                Array.Clear(arrList[i], 0, arrList[i].Length);
            }
        }

        public BigArray(long size, long lowBounding)
        {
            size_ = size;
            lowBounding_ = lowBounding;
            arrList = new List<T[]>();

            for (long i = 0; i < size_; i += sizePerBlock)
            {
                if (i + sizePerBlock < size_)
                {
                    arrList.Add(new T[sizePerBlock]);
                }
                else
                {
                    arrList.Add(new T[size_ - i]);
                }
            }
        }
    }
}
